﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PoolGame2.Menus
{
    public class MenuItem : GameComponent
    {
        public EventHandler onSelected;

        private SpriteFont Font;
        string text;
        float flScale = 1.0f;
        bool isSelected;
        Vector2 position;
        bool visible = true;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public bool Visible
        {
            get { return visible; }
            set { visible = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public virtual void OnEntry() { isSelected = true; flScale = 1.5f; }
        public virtual void OnSelect() { onSelected.Invoke( this, null ); }
        public virtual void OnLeave() { isSelected = false; flScale = 1.0f; }

        public MenuItem(string text, PoolGame game) : base(game)
        {
            //Font = game.fontDatabase.loadFont("Courier New");
            Font = game.fontDatabase.loadFont("Arial");
            this.text = text;
        }

        public override void Update(GameTime gameTime)
        {
        }


        public virtual void Draw(GameTime gameTime, Vector2 offset)
        {
            if (visible == false)
                return;

            float alpha = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds * 6) + 1;
            Color color = Color.Black;
            if( isSelected )
                color = new Color(Color.Black.R, Color.Black.G, Color.Black.B, alpha);

            PoolGame pGame = (PoolGame)Game;
            SpriteBatch spriteBatch = pGame.spriteBatch;
            Texture2D menuBar = pGame.textureDatabase.GetTexture("menu_bar");

            Vector2 origin = new Vector2(0, Font.LineSpacing / 2);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
           
            spriteBatch.Draw(menuBar, offset + position - new Vector2(20,30), null, Color.White);

            spriteBatch.DrawString(Font, text, offset + position, Color.Black, 0,
                                   origin, flScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }

        public virtual int GetHeight()
        {
            return Font.LineSpacing;
        }
    }
}
